package objects
{
	import flash.display.Bitmap;
	import flash.display.DisplayObjectContainer;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.filters.GlowFilter;
	import flash.text.Font;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	/**
	 * use getter scaleLoader to animate
	 * @author Ja
	 */
	public class LoaderComponent extends Sprite implements ILoaderComponent
	{
		//embed font with range of characters
		// Font used by the GUI
		[Embed(
			systemFont = 'Calibri',
			embedAsCFF = 'false',
			fontName = 'embeddedFont',
			mimeType = 'application/x-font-truetype',
			unicodeRange='U+0030-0039, U+0025-0025')]
		private var EMBEDDED_FONT:Class;
		private var myFont:Font = new EMBEDDED_FONT();
		
			/*
			 unicodeRange:
           U+0041-005A,  Upper-Case [A..Z] 
           U+0061-007A,  Lower-Case a-z 
           U+0030-0039,  Numbers [0..9] 
           U+002E-002E;  Period [.] 
		   U+0025-0025    [%]
			*/
		
		[Embed(source="../../assets/loader-empty.png")]
		private var LoaderEmpty:Class;
		private var loaderEmpty:Bitmap = new LoaderEmpty();
		
		[Embed(source="../../assets/loader-full.png")]
		private var LoaderFull:Class;
		private var loaderFull:Bitmap = new LoaderFull();
		
		private var _mask:Shape;
		private var _scaleLoader:Number = 0;
		
		private var _txtFld:TextField;
		private var _txtFormat:TextFormat;
		
		private var _glowON:Boolean = false;
		
		/**
		 * use getter scaleLoader to animate
		 */
		public function LoaderComponent(glowON:Boolean=true)
		{
			_glowON = glowON;
			addChild(loaderEmpty);
			addChild(loaderFull);
			setMask();
			setTXT();
			this.mouseChildren = false;
			this.mouseEnabled = false;
		}
		
		
		
		private function setTXT():void
		{
			_txtFld = new TextField();
			_txtFld.autoSize = TextFieldAutoSize.LEFT;
			_txtFormat = new TextFormat();
			_txtFld.embedFonts = true;
			_txtFormat.color = 0xFFFFFF;
			_txtFormat.font = myFont.fontName;
			_txtFormat.letterSpacing = 2;
			_txtFormat.size = 16;
			_txtFld.defaultTextFormat = _txtFormat;
			
			_txtFld.text = "0%";
			_txtFld.x = int((loaderEmpty.width * 0.774) >> 1) - (_txtFld.width >> 1);
			_txtFld.y = int(this.height >> 1) - (_txtFld.height >> 1);
			
			_txtFld.filters = [new GlowFilter(0xFFFFFF,1,8,8,1)]
			_txtFld.mouseEnabled = false;
			addChild(_txtFld);
			
		}
		
		private function setMask():void
		{
			_mask = new Shape();
			_mask.graphics.beginFill(0xff0000, 0.5);
			_mask.graphics.drawRect(0, 0, loaderEmpty.width * 0.774, loaderEmpty.height);
			_mask.graphics.endFill();
			_mask.scaleX = _scaleLoader;
			addChild(_mask);
			loaderFull.mask = _mask;
		}
		
		public function reset():void 
		{
			scaleLoader = 0;
		}
		
		public function get scaleLoader():Number
		{
			if (_scaleLoader <= 0)
				return 0;
			if (_scaleLoader >= 1)
				return 1;
			//_txtFld.text = Number(_scaleLoader * 100+0.5).toFixed(0) + "%";
			return _scaleLoader;
		}
		
		public function set scaleLoader(value:Number):void
		{
			_scaleLoader = value;
			if (_mask)
			{
				value >= 0.99 ? _mask.scaleX = 1.27 : _mask.scaleX = _scaleLoader;
			}
			_txtFld.text = Number(_scaleLoader * 100).toFixed(0) + "%";
		
		}
		
		public function dispose():void
		{
			removeChild(loaderEmpty);
			loaderEmpty.bitmapData.dispose();
			loaderEmpty = null;
			LoaderEmpty = null;
			
			removeChild(loaderFull);
			loaderFull.bitmapData.dispose();
			loaderFull = null;
			LoaderFull = null;
			
			parent.removeChild(this);
		
		}
	
	}

}